<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue set的使用</title>
    <script src="js/vue.js"></script>
</head>

<body>
    注意对象不能是 Vue 实例，或者 Vue 实例的根数据对象。 例如 data
    <div id="root">
        <button @click.once="fun">点我增加学生的性别</button>
        <ul>
            <li v-for="s of students" :key="s.id">
                {{s.id}}---{{s.name}}---{{s.age}} ---{{s.sex}}
            </li>
        </ul>
    </div>
</body>
<script>
    let vm1 = new Vue({
        el: "#root",
        data() {
            return {
                students: [
                    { id: "001", name: "张三", age: 19 },
                    { id: "002", name: "李四", age: 20 },
                    { id: "003", name: "王五", age: 21 },
                ]
            }
        },
        methods: {
            fun() {
                for (const index in this.students) {
                    this.$set(this.students[index], "sex", "男")
                }
                console.log("添加完成");
            }
        }
    })

    // //方法一
    // Vue.set(vm1._data.students[0], "sex", '男')

    // //方法二
    // Vue.set(vm1.students[0], "sex", "男")

    // //方法三
    // vm1.$set(vm1._data.students[0], "sex", "男")

    // //方法四
    // vm1.$set(vm1.students[0], "sex", "男")

    //不能做的操作 不能为_data添加
    // vm1.$set(vm1._data, "text", "aaa")
    //vue.js:634 [Vue warn]: Avoid adding reactive properties to a Vue
    // instance or its root $data at runtime - declare it upfront in the data option.
</script>

</html>